import { defineConfig, ChangeCase } from 'yapi-to-typescript';

const getName = (dir: string, name: string, changeCase: ChangeCase) => {
  const dirName = dir
    .split('/')
    .filter((v) => v.includes('{') === false)
    .join(' ');
  name = (name || '').includes('{') ? 'by ' + name : name;
  return changeCase.camelCase([dirName, name].join(' '));
};

function getProjectConfig(token: string) {
  return {
    token,
    categories: [
      {
        id: 0,
        getRequestDataTypeName(interfaceInfo, changeCase) {
          const { name, dir } = interfaceInfo.parsedPath;
          return (
            'I' +
            changeCase.pascalCase(interfaceInfo.method) +
            changeCase.pascalCase(getName(dir, name, changeCase) + 'Req')
          );
        }
      }
    ]
  };
}

export default defineConfig([
  {
    // serverUrl: 'http://10.10.12.184:30500',
    serverUrl: 'https://yapi.mcisaas.com',
    serverType: 'yapi',
    typesOnly: false,
    target: 'typescript',
    reactHooks: {
      enabled: false
    },
    devEnvName: 'local',
    prodEnvName: 'production',
    outputFilePath: (interfaceInfo) => {
      return `src/api/${interfaceInfo._project.name}.ts`;
    },
    requestFunctionFilePath: 'src/utils/request.ts',
    dataKey: 'data',
    comment: {
      requestHeader: true
    },
    getResponseDataTypeName(interfaceInfo, changeCase) {
      const { name, dir } = interfaceInfo.parsedPath;
      return (
        'I' + changeCase.pascalCase(interfaceInfo.method) + changeCase.pascalCase(getName(dir, name, changeCase) + 'Res')
      );
    },
    getRequestFunctionName(interfaceInfo, changeCase) {
      let { dir, name } = interfaceInfo.parsedPath;
      return changeCase.camelCase([interfaceInfo.method, getName(dir, name, changeCase)].join(' '));
    },
    projects: [
      // numa-operation
      getProjectConfig('9f435758a4ba339ee4899dbe3b3940d1a3d831034196ca0c2bdf97cca0bda7fa'),
      // 决策引擎
      getProjectConfig('3b2c1027d5e271eda302946e68790e2e890de52d204a2b39de1fe29605e7dbf1')
    ]
  }
]);
